package com.hlf.modules.merchant.printer.controller;

/**
 * Created by Administrator on 2017/10/27.
 */


import com.hlf.common.annotation.SysLog;
import com.hlf.common.utils.PageUtils;
import com.hlf.common.utils.ReturnResult;
import com.hlf.common.validator.Assert;
import com.hlf.modules.api.entity.Print;
import com.hlf.modules.api.service.MachineService;
import com.hlf.modules.sys.controller.AbstractController;
import com.hlf.modules.sys.entity.SysUserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 设备接口
 */
@RestController
@RequestMapping("/machine")
public class MachineController extends AbstractController{

    @Autowired
    private MachineService machineService;


    /**
     * 打印机列表
     */
    @RequestMapping("list")
    public ReturnResult list(@RequestParam Map<String, Object> params) {
        SysUserEntity user = getUser();
        PageUtils pageUtils = machineService.queryPrintList(user, params);
        return ReturnResult.ok().put("page", pageUtils);
    }

    /**
     * 添加打印机
     */
    @SysLog("添加打印机")
    @RequestMapping("add")
    public ReturnResult add(@RequestBody Print print) {
        Assert.isNull(print.getName(),"打印机名称不能为空!");
        Assert.isNull(print.getPrintType(),"打印类型不能为空!");
        Assert.isNull(print.getPrintNum(),"打印数量不能为空!");
        Assert.isNull(print.getMachineNum(),"打印机机器号不能为空!");
        Assert.isNull(print.getPrintKey(),"打印机秘钥不能为空!");
        SysUserEntity user = getUser();
        return machineService.addPrint(user, print);
    }

    /**
     * 禁用/解绑/启用
     */
    @SysLog("禁用/解绑/启用打印机")
    @RequestMapping("update")
    public ReturnResult update(@RequestBody Print print) {
        Assert.isNull(print.getId(),"打印机id不能为空!");
        Assert.isNull(print.getUseStatus(),"使用状态不能为空!");
        SysUserEntity user = getUser();
        machineService.useOrFree(user,print);
        return ReturnResult.ok();
    }


    /**
     * 查询单个打印机
     */
    @RequestMapping("info/{id}")
    public ReturnResult info(@PathVariable(value = "id")String id) {
        Assert.isNull(id,"打印机id不能为空!");
        Print printer = machineService.queryObject(Long.parseLong(id));
        return ReturnResult.ok().put("printer",printer);
    }


}
